#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@fileName     : config.py
@description  : 处理配置文件
@time         : 2021年04月10日 10:02
@author       : WeiTingjun
@versions     : 1.0
'''
import os
from configparser import ConfigParser

config_name = 'config.ini'
default_file_path = os.path.join(os.path.dirname(__file__), config_name)


class Config(object):
    def __init__(self):
        self.config_file_path = default_file_path
        self.config = ConfigParser()
        self.config.read(self.config_file_path)

    def get_section(self, node_section='AccessToken', *args):
        """
        获取节点的信息
        :param node_section:节点名称
        :param args:
        :return: 返回字典
        """
        res_dict = {}
        for value in args:
            res_dict[value] = self.config.get(node_section, value)
        return res_dict

    def update_section(self, node_section='AccessToken', **kwargs):
        """
        更新AccessToken节点
        :param node_section:节点名称
        :param kwargs:
        :return:无返回
        """
        if self.config.has_section(node_section):
            self.config.remove_section(node_section)
        self.config.add_section(node_section)
        for key, value in kwargs.items():
            self.config.set(node_section, key, value)
        self.config.write(open(self.config_file_path, 'w'))


class WechatConfigObject(Config):
    """
    调企业微信需要的参数
    """

    def __init__(self):
        super(WechatConfigObject, self).__init__()
        token_info = self.get_section('AccessToken',
                                      'corpid',
                                      'secret',
                                      'expires_in',
                                      'access_token',
                                      'agentid',
                                      'userid')
        self.corp_id = token_info.get('corpid')
        self.access_token = token_info.get('access_token')
        self.to_users = token_info.get('userid')
        self.agent_id = token_info.get('agentid')
        self.token_indate = token_info.get('expires_in')
        self.secret = token_info.get('secret')
